package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo7 {
    public static void main(String[] args) {
        //需求:查看1年级1班的学生信息
        /*
            SELECT s.name,s.gender,s.age,s.job,c.name,c.floor
            FROM class c
            JOIN student s ON c.id = s.class_id
            WHERE c.name='1年级1班'
         */
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "SELECT s.name,s.gender,s.age,s.job,c.name,c.floor " +
                         "FROM class c " +
                         "JOIN student s ON c.id = s.class_id " +
                         "WHERE c.name='1年级1班'";
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("学生姓名--性别--年龄--职位--班级名--楼层");
            while(rs.next()){
                String sName = rs.getString("s.name");
                String sGender = rs.getString("s.gender");
                int sAge = rs.getInt("s.age");
                String sJob = rs.getString("s.job");
                String cName = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
                System.out.println(sName+"--"+sGender+"--"+sAge+"--"+sJob+"--"+cName+"--"+cFloor);
            }


        } catch (SQLException e) {
           e.printStackTrace();
        }

    }
}
